package com.chuanjiabao.wisdom.util.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.View;

import com.bumptech.glide.Glide;
import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.databinding.DialogDownloadProgressHintBinding;

import java.util.concurrent.TimeUnit;

import io.reactivex.Completable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class DowloadDialog extends BaseDialog {

    private Context context;
    private DialogDownloadProgressHintBinding binding;

    public DowloadDialog(Context context) {
        super(context, R.style.NoAnimeDialog);
        this.context = context;
        binding = DataBindingUtil.inflate(LayoutInflater.from(context),
                R.layout.dialog_download_progress_hint, null, false);
        binding.setDialog(this);
        setContentView(binding.getRoot());
        setCanceledOnTouchOutside(false);
        setCancelable(false);
    }

    @SuppressLint("CheckResult")
    public void setProgress(int progress) {
        if (progress >= 100) {
            binding.cpModuleProgress.setVisibility(View.INVISIBLE);
            Glide.with(context)
                    .asGif()
                    .load(R.mipmap.icon_save_done)
                    .into(binding.ivModuleDone);

            //noinspection ResultOfMethodCallIgnored
            Completable.timer(700, TimeUnit.MILLISECONDS)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this::dismiss);
        } else binding.cpModuleProgress.setProgress(progress);
    }

}


